package com.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pojo.Archive;
import com.service.ArchiveService;
import com.service.LogService;
import com.service.UsersService;

@Controller
public class ArchiveController {
	UsersService us = new UsersService();
	ArchiveService achs = new ArchiveService();
	LogService logs = new LogService();

	/**
	 * 添加档案
	 */
	@RequestMapping(value = "/add.action", method = RequestMethod.POST)
	@ResponseBody
	public String add(
			@CookieValue(value = "users", defaultValue = "") String userid,
			@RequestParam(value = "archive_id", defaultValue = "") String archive_id,
			@RequestParam(value = "identity", defaultValue = "") String identity,
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "gender", defaultValue = "") String gender,
			@RequestParam(value = "birthday", defaultValue = "") String birthday,
			@RequestParam(value = "date_retire1", defaultValue = "") String date_retire1,
			@RequestParam(value = "date_retire2", defaultValue = "") String date_retire2,
			@RequestParam(value = "sp", defaultValue = "") String sp,
			@RequestParam(value = "fc", defaultValue = "") String fc,
			@RequestParam(value = "title", defaultValue = "") String title,
			@RequestParam(value = "seniority", defaultValue = "") String seniority) {
		// System.out.println(archive_id);
		achs.add(archive_id, identity, name, gender, birthday, date_retire1,
				date_retire2, sp, fc, title, seniority);
		Map<String, Object> map = achs.lastId();
		logs.add(userid, map.get("id").toString(), "添加");
		return "OK";
	}

	/**
	 * 模糊查询档案
	 */
	@RequestMapping(value = "/query.action", method = RequestMethod.POST)
	@ResponseBody
	public List<Archive> query(
			@CookieValue(value = "id", defaultValue = "") String id,
			@CookieValue(value = "users", defaultValue = "") String userid,
			@RequestParam(value = "archive_id", defaultValue = "") String archive_id,
			@RequestParam(value = "identity", defaultValue = "") String identity,
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "gender", defaultValue = "") String gender,
			@RequestParam(value = "birthday", defaultValue = "") String birthday,
			@RequestParam(value = "date_retire1", defaultValue = "") String date_retire1,
			@RequestParam(value = "date_retire2", defaultValue = "") String date_retire2,
			@RequestParam(value = "sp", defaultValue = "") String sp,
			@RequestParam(value = "fc", defaultValue = "") String fc,
			@RequestParam(value = "title", defaultValue = "") String title,
			@RequestParam(value = "seniority", defaultValue = "") String seniority,
			HttpServletResponse response) {
		List<Archive> arc = achs.query(userid, archive_id, identity, name,
				gender, birthday, date_retire1, date_retire2, sp, fc, title,
				seniority);
		// System.out.println(arc);
		return arc;
	}

	/**
	 * 根据archive_id和identity查询一条档案
	 */
	@RequestMapping(value = "/queryone.action", method = RequestMethod.POST)
	@ResponseBody
	public String queryone(
			@RequestParam(value = "search", defaultValue = "") String search,
			HttpServletResponse response) {
		// System.out.println(search);
		List<Archive> arc = achs.queryone(search);
		// System.out.println(arc);
		if (0 == arc.size()) {
			return "ERR1";
		} else if (1 < arc.size()) {
			return "ERR2";
		}
		return arc.get(0).getId();
	}

	/**
	 * 根据archive_id和identity查询一条档案在页面显示
	 */
	@RequestMapping(value = "/query_one.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Archive> query_one(
			@CookieValue(value = "users", defaultValue = "") String userid,
			@RequestParam(value = "id", defaultValue = "") String id) {
		// System.out.println(id);
		List<Archive> arc = achs.query_one(id);
		logs.add(userid, arc.get(0).getId(), "浏览");
		// System.out.println(arc);
		return arc;
	}

	/**
	 * 根据archive_id和identity查询一条档案做修改
	 */
	@RequestMapping(value = "/update.action", method = RequestMethod.POST)
	@ResponseBody
	public String update(
			@CookieValue(value = "users", defaultValue = "") String userid,
			@RequestParam(value = "id", defaultValue = "") String id,
			@RequestParam(value = "archive_id", defaultValue = "") String archive_id,
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "gender", defaultValue = "") String gender,
			@RequestParam(value = "birthday", defaultValue = "") String birthday,
			@RequestParam(value = "date_retire1", defaultValue = "") String date_retire1,
			@RequestParam(value = "date_retire2", defaultValue = "") String date_retire2,
			@RequestParam(value = "sp", defaultValue = "") String sp,
			@RequestParam(value = "fc", defaultValue = "") String fc,
			@RequestParam(value = "title", defaultValue = "") String title,
			@RequestParam(value = "seniority", defaultValue = "") String seniority) {
		//System.out.println(id);
		achs.update(id, name, gender, birthday, date_retire1, date_retire2, sp,
				fc, title, seniority);
		logs.add(userid, id, "修改");
		return "OK";
	}

	/**
	 * 查询最近浏览的四条档案 在主页显示
	 */
	@RequestMapping(value = "/queryliu.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, Object>> queryliu(
			@CookieValue(value = "users", defaultValue = "") String userid,
			HttpServletResponse response) {
		List<Map<String, Object>> list = achs.queryliu(userid);
		return list;
	}

	/**
	 * 查询最近添加的四条档案 在主页显示
	 */
	@RequestMapping(value = "/queryadd.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, Object>> queryadd(
			@CookieValue(value = "users", defaultValue = "") String userid) {
		List<Map<String, Object>> list = achs.queryadd(userid);
		return list;
	}

	/**
	 * 统计每个用户上传档案的总数
	 */
	@RequestMapping(value = "/querycount.action", method = RequestMethod.POST)
	@ResponseBody
	public List<Map<String, Object>> querycount(
			@RequestParam(value = "date1", defaultValue = "") String date1,
			@RequestParam(value = "date2", defaultValue = "") String date2) {
		// System.out.println(date1+" "+date2);
		List<Map<String, Object>> list = achs.querycount(date1, date2);
		return list;
	}

}
